package dao

import (
	"go-fitty/configs"
	"go-fitty/internal/model"
	"log"
)

type UserDao struct {
}

// 新增
func (p *UserDao) Insert(user *model.User) int64 {
	engine := configs.InstanceMaster()
	v, err := engine.Insert(user)
	if err != nil {
		log.Fatalf("插入数据出现错误，%v", err)
		return -1
	}
	return v
}

// 删除
func (p *UserDao) Delete(id int64) int64 {
	var user model.User
	engine := configs.InstanceMaster()
	v, err := engine.ID(id).Delete(&user)
	if err != nil {
		log.Fatalf("插入数据出现错误，%v", err)
		return -1
	}
	return v
}

// 修改
func (p *UserDao) Update(user *model.User) int64 {
	engine := configs.InstanceMaster()
	v, err := engine.ID(user.Id).Update(user)
	if err != nil {
		log.Fatalf("修改数据出现错误，%v", err)
		return -1
	}
	return v
}

// 查找
func (p *UserDao) FindOne(id int64) *model.User {
	var user model.User
	engine := configs.InstanceMaster()
	_, err := engine.ID(id).Get(&user)
	if err != nil {
		log.Fatalf("查找数据时出现错误，%v", err)
	}
	return &user
}
